//
//  WifiInfoView.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/5/19.
//

import SwiftUI
import FlowStacks

struct WifiInfoView: View {
    @EnvironmentObject var navigator: FlowNavigator<HomeStack>
    @StateObject private var viewModel = WifiInfoViewModel()
    let closeAction: () -> Void
    
    var body: some View {
        VStack{
            NavigationBar(title: "配置WIFI", isBackBtnVisible: false, onBack: {
                closeAction()
            }, trailing:
                AnyView(
                    HStack {
                        Button(action: {
                            closeAction()
                            }, label: {
                                Image("close")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 24, height: 24)
                            }).padding(.trailing, 12)
                    }.padding(.trailing, 16)
                ))
         
            Text("请选择 2.4GHz WiFi 网络并输入密码")
                .font(.system(size: 16, weight: .semibold))
                .foregroundColor(AppColors.Gy1)
                .padding(.top, 44)
            Text("如果你的 WiFi 是 5GHz 的，请先设置为2.4GHz")
                .font(.system(size: 16, weight: .regular))
                .foregroundColor(AppColors.Gy3)
                .padding(.top, 8)
            Image("wifi_setting")
                .resizable()
                .scaledToFit()
                .padding(.top, 24)
            
            HStack {
                Image("ic_wifi")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 20, height: 20)
                    .padding(.leading, 12)
                Text(viewModel.currentWifiSSID)
                    .font(.system(size: 16, weight: .regular))
                    .foregroundColor(AppColors.Gy1)
                    .padding(.leading, 10)
                Spacer()
                Button(action: {
                    PermissionManager.shared.openAppSettings()
                    }, label: {
                        Text("切换WIFI")
                            .font(.system(size: 14, weight: .bold))
                            .foregroundColor(AppColors.Brand6)
                    }).padding(.trailing, 12)
                
                
            }.frame(height: 48)
            .background(AppColors.Gray1)
                .cornerRadius(6)
                .padding(.horizontal, 24)
                .padding(.top, 36)
            HStack {
                Image("ic_lock")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 20, height: 20)
                    .padding(.leading, 12)
                
                Spacer()
                CustomSecureField(
                    text: $viewModel.password,
                    placeholder: "password",
                    isSecure: $viewModel.isPasswordHidden
                ).padding(.trailing, 12)
                
            }.frame(height: 48)
            .background(AppColors.Gray1)
                .cornerRadius(6)
                .padding(.horizontal, 24)
                .padding(.top, 12)
            
            Button(action: {
                let result = viewModel.saveWifiInfo()
                if result {
                    closeAction()
                }
            }) {
                   Text("下一步")
                       .font(.system(size: 16, weight: .semibold))
                       .foregroundColor(.white)
                       .frame(maxWidth: .infinity, minHeight: 48)
                       .background(
                           LinearGradient(
                               gradient: Gradient(colors: [
                                Color(hex: "#9470FF"),
                                   Color(hex: "#6652F9")
                                   
                               ]),
                               startPoint: .leading,  // 270deg 相当于从右向左
                               endPoint: .trailing
                           )
                       )
                       .cornerRadius(45)
            }.padding(.top, 36)
                .padding(.horizontal, 24)
            
            Spacer()
            
            
        }  .onActiveRefresh {
            viewModel.refreshCurrentWifiSSID()
        }
    }
}
